<template>
	<view class="page">
		<topVue title="兑换中心" name="order" @handle="$tools.route('./exchangeRecord')"></topVue>
		<!-- banner部分 -->
		<view class="bannerVue margin0 padding30">
			<view class="label">可用绿色积分</view>
			<u-count-to
				:start-val="0" 
				:end-val="totalPoints/100"
				font-size="42"
				color="#fff"
				:bold="true"
				separator=","
			></u-count-to>
			<view class="label">冻结积分: {{freezePoints/100}}</view>
		</view>
		
		<view class="noticeVue padding30 margin20 flex align-center">
			<u-icon name="info-circle" color="#1CC289" size="40"></u-icon>
			<view class="notice">
				兑换申请提交后，积分将进入冻结状态。通过审核后，积分将被扣除；审核未通过，积分将解除冻结;审核在工作日48小时内完成。
			</view>
		</view>
		<view class="inpVue">
			<view class="title">兑换编号</view>
			<input type="number" placeholder="请输入交易账号" v-model="account"/>
		</view>
		<view class="inpVue">
			<view class="title">兑换积分数量</view>
			<input type="number" placeholder="输入兑换积分数量(最低100且为100的整数倍)" v-model="quantity"/>
		</view>
		
		<view class="inpVue" @click="show=true">
			<view class="title">兑换类型</view>
			<view class="relative">
				<input type="text" placeholder="请选择兑换类型" v-model="type" disabled/>
				<image src="../../static/more.png" style="width: 40rpx; height: 40rpx; right: 20rpx" class="absolute-middle"></image>
			</view>
		</view>
		<view class="btn margin50" @click="submit()">提交兑换</view>
		
		<Loading :num="2" v-if="loading"></Loading>
		<u-action-sheet :list="types" v-model="show" @click="confirm"></u-action-sheet>
	</view>
</template>

<script>
	import { mapState } from "vuex"
	export default {
		data(){
			return {
				quantity: "",
				account: "",
				totalPoints: 0,
				freezePoints: 0,
				types: [{text: "绿票"}],
				type: "",
				show: false,
				loading: false
			}
		},
		
		computed: {
			...mapState({
				user: state => state.user,
				balanceInfo: state => state.balanceInfo
			})
		},
		
		async mounted() {
			await this.getBalance()
		},
		methods: {
			async getBalance(){
				const res = await this.$request("/app-api/pay/wallet/earnUserPoints", "GET", {
					userId: this.user.id
				})
				if(res.code===0) {
					console.log(res)
					this.freezePoints = res.data.freezePoints
					this.totalPoints = res.data.totalPoints
				}
			},
			
			confirm(e){
				this.type = this.types[e].text
			},
			
			// 提交兑换
			async submit(){
				if(!this.account) return this.$u.toast("请输入交易账号")
				if(!this.quantity || this.quantity==0) return this.$u.toast("请输入兑换积分数量")
				if(this.quantity <100) return this.$u.toast("换积分数量最低100")
				if(this.quantity%100!=0) return this.$u.toast("数量为100整数倍")
				if(!this.type) return this.$u.toast("请选择兑换类型")
				if(this.totalPoints < this.quantity) return this.$u.toast("积分不足")
				this.loading = true
				const res = await this.$request("/app-api/pay/wallet/submitTheRedemption", "POST", {
					userId: this.user.id,
					transactionNumber: this.account,
					numberOfPoints: this.quantity,
					redemptionType: this.type
				})
				this.loading = false
				if(res.code===0) {
					this.$u.toast("兑换成功")
					this.type = ""
					this.account = ""
					this.quantity = ""
				}else{
					this.$u.toast(res.msg)
				}
			}
		}
	}
</script>

<style lang="less" scoped>
	.bannerVue {
		width: 690rpx;
		background: linear-gradient(to right, #1CC289, #60A5FA);
		border-radius: 18rpx;
		.label {
			color: #fff;
			line-height: 1;
			padding: 15rpx 0;
		}
		.using {
			color: #fff;
			font-weight: 600;
			font-size: 46rpx;
			line-height: 1;
			padding: 30rpx 0;
		}
	}
	
	.noticeVue {
		width: 690rpx;
		background: #f5f5f5;
		border-radius: 15rpx;
		.notice {
			color: #333;
			padding-left: 15rpx;
			line-height: 42rpx;
			font-weight: 600;
		}
	}
	
	.inpVue {
		width: 100%;
		padding: 30rpx 30rpx 0;
		.title {
			color: #333;
			font-weight: 600;
			font-size: 30rpx;
		}
		input {
			flex: 1;
			height: 92rpx;
			border: 1rpx solid #f5f5f5;
			border-radius: 15rpx;
			display: block;
			padding-left: 20rpx;
			color: #333;
			font-size: 28rpx;
			margin-top: 20rpx;
		}
	}
	
	.btn {
		width: 690rpx;
		height: 92rpx;
		background: #1CC289;
		border-radius: 15rpx;
		line-height: 92rpx;
		text-align: center;
		color: #fff;
		font-size: 30rpx;
		font-weight: 600;
	}
</style>